<?php
/**
 * [YIZHIKEJI System] Copyright (c) 2014 yz929.cn
 * YIZHIKEJI is NOT a free software, it under the license terms, visited http://www.yz929.cn/ for more details.
 */
defined('IN_IA') or exit('Access Denied');
load()->model('mc');
/* $_W['uniacid'] = intval($_GPC['i']);
 if(empty($_W['uniacid'])) {
	$_W['uniacid'] = intval($_GPC['weid']);
}
$_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);
if(empty($_W['uniaccount'])) {
	exit('指定主公众号不存在。');
}
if (!empty($_W['uniaccount']['endtime']) && TIMESTAMP > $_W['uniaccount']['endtime']) {
	exit('抱歉，您的公众号服务已过期，请及时联系管理员');
} */

$_W['multiid'] = intval($_GPC['multiid']);
$_W['account'] = pdo_get('wechat_account');
$_W['state'] = 'baseweb';


session_start();
//开发时使用
//$_SESSION['openid'] = $_W['openid'] = 'oFcCbxPOy7rPs5oB7NnUK6sMEm4Q';
$_W['openid'] = $_SESSION['openid'] ;
if(empty($_W['openid'])) {
//微信进入
    if (!empty($_GPC['__auth'])) {
        $auth = @json_decode(base64_decode($_GPC['__auth']), true);
        $openid = $auth['openid'];
        $_W['openid'] = $_SESSION['openid'] = $openid;


    } elseif ($_GPC['state'] == $_W['state']) {
        // 网页base授权
        $code = $_GPC['code'];
        $wei_account = WeAccount::create($_W['account']);
        $content = $wei_account->getOauthInfo($code);
        $_W['openid'] = $_SESSION['openid'] = $content['openid'];
        $fans = mc_fan_info($_W['openid']);
        if (empty($fans)) { // 数据库没有记录，没有关注
            template('auth/register');
            exit("请先关注公众号");
        }
    } else {
        $url = urlencode($_W['siteurl']);
        $wei_account = WeAccount::create($_W['account']);
        $forward = $wei_account->getOauthCodeUrl($url, $_W['state']);
        header('Location:' . $forward);
        exit;
    }
}

//到这里拿到openid
//ownerid 为微站商家的id， uid 为当前访问的用户id(粉丝表)
$_W['fans'] = mc_fan_info($_W['openid']);
$_W['ownerid'] = pdo_fetchcolumn('select uid from '.tablename('site_multi').' where id=:id',array(':id'=>$_W['multiid']));
$_W['uid'] = $_W['fans']['uid'];

//网站用户的 id
$_W['web_uid'] = $_GPC['web_uid'];

//自动成为微站下面的粉丝
mc_mapping_fans_update();

/*  if (!empty($_W['account']['oauth']) && $_W['account']['oauth']['level'] == '4') {
	if (($_W['container'] == 'wechat' && !$_GPC['logout'] && empty($_W['openid']) && ($controller != 'auth' || ($controller == 'auth' && !in_array($action, array('forward', 'oauth'))))) ||
		($_W['container'] == 'wechat' && !$_GPC['logout'] && empty($_SESSION['oauth_openid']) && ($controller != 'auth'))) {
		$state = 'we7sid-'.$_W['session_id'];
		if (empty($_SESSION['dest_url'])) {
			$_SESSION['dest_url'] = urlencode($_W['siteurl']);
		}
		$str = '';
		if(uni_is_multi_acid()) {
			$str = "&j={$_W['acid']}";
		}
		$url = (!empty($unisetting['oauth']['host']) ? ($unisetting['oauth']['host'] . $sitepath . '/') : $_W['siteroot'] . 'app/') . "index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=snsapi_base";
		$callback = urlencode($url);
		$oauth_account = WeAccount::create($_W['account']['oauth']);
		$forward = $oauth_account->getOauthCodeUrl($callback, $state);
		header('Location: ' . $forward);
		exit();
	}
}

$_W['account']['groupid'] = $_W['uniaccount']['groupid'];
$_W['account']['qrcode'] = tomedia('qrcode_'.$_W['acid'].'.jpg').'?time='.$_W['timestamp'];
$_W['account']['avatar'] = tomedia('headimg_'.$_W['acid'].'.jpg').'?time='.$_W['timestamp'];
if ($_W['container'] == 'wechat') {
	if ($_W['account']['level'] < 3) {
		if (!empty($unisetting['jsauth_acid'])) {
			$jsauth_acid = $unisetting['jsauth_acid'];
		} elseif (!empty($unisetting['oauth']['account'])) {
			$jsauth_acid = $unisetting['oauth']['account'];
		}
	} else {
		$jsauth_acid = $_W['acid'];
	}
	if (!empty($jsauth_acid)) {
		$accountObj = WeAccount::create($jsauth_acid);
		$_W['account']['jssdkconfig'] = $accountObj->getJssdkConfig();
		$_W['account']['jsauth_acid'] = $jsauth_acid;
	}
	unset($jsauth_acid, $accountObj);
}

$_W['card_permission'] = 0;
if($_W['acid'] && $_W['account']['level'] >= 3 && $_W['container'] == 'wechat') {
	$_W['card_permission'] = 1;
} */
load()->func('compat.biz');